#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int T;

  for (cin >> T; T; T--) {
    int n;

    cin >> n;
    vector<int> a(n), b(n);

    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    for (int i = 0; i < n; i++) {
      cin >> b[i];
    }
    bool succ = (a.back() == b.back());

    for (int i = n - 2; i >= 0 && succ; i--) {
      if ((a[i] ^ a[i + 1]) != b[i] && (a[i] ^ b[i + 1]) != b[i] && a[i] != b[i]) {
        succ = false;
      }
    }
    cout << (succ ? "YES\n" : "NO\n");
  }
  return 0;
}
